#class Name: Vnode
#author:jch
#date:2009-06-21

####import part start
from xml.dom import minidom
import urllib
####import part end



####################define class Vnode
class Vnode:
      def __init__(self,vnode_xml):
          self.describleXML = vnode_xml;
          self.memory="";
          self.diskSize="";
          self.capability = None;
          self.hostname = "";
          self.passwd = "";
          self.diskRef = "";
          self.memRef = "";
          self.templateRef = "";
          self.networkSources = [];
          self.deployMethod = "";
          self.downloadLink = "";
          self.cowdir = "";
          self.osType = "";
          
      def basicParse(self):
          xmldoc = minidom.parseString(self.describleXML);
          self.templateRef = xmldoc.getElementsByTagName("vTemplateRef")[0].firstChild.data
          self.templateRef = self.templateRef.strip();
          
          self.hostname = xmldoc.getElementsByTagName("Hostname")[0].firstChild.data
          self.hostname = self.hostname.strip();
          self.diskRef = xmldoc.getElementsByTagName("DiskRef")[0].firstChild.data 
          self.memRef = xmldoc.getElementsByTagName("MemRef")[0].firstChild.data
          if (xmldoc.getElementsByTagName("Password")[0] != None) :
              self.passwd = xmldoc.getElementsByTagName("Password")[0].firstChild.data
          memory = xmldoc.getElementsByTagName("Mem")[0].firstChild.data
          memory = memory.strip()
          memory = memory.lower()
          if not memory.isdigit():
             if memory[-1]=='g':
                memory = int(memory[0:len(memory)-1])*1024*1024
             elif memory[-1]=='m':
                memory = int(memory[0:len(memory)-1])*1024
             else:
                memory = int(memory[0:-1])
          else:
             memory = int(memory)*1024

          memory = str(memory)
          self.memory = memory;
          
          self.diskSize = xmldoc.getElementsByTagName("DiskSize")[0].firstChild.data
          self.diskSize = self.diskSize.strip()
          self.diskSize = self.diskSize[0:len(self.diskSize)-1]
          
          #self.capability=""
          self.networkSources = []
          nics = xmldoc.getElementsByTagName("NIC")
          for i in range(len(nics)):
               _uuid = nics[i].getElementsByTagName("vSwitchRef")[0].firstChild.data;
               _uuid = _uuid.strip();
               _id =  nics[i].attributes["id"].value
               self.networkSources.append(_uuid)
          
          
      def detailParse(self):
          if self.templateRef == "":
             return ;
          fsock = urllib.urlopen(self.templateRef)
          xmldoc =minidom.parse(fsock)
          deployInfo = xmldoc.getElementsByTagName("DeployInfo")[0]
          self.deployMethod = deployInfo.getElementsByTagName("Method")[0].firstChild.data
          self.downloadLink = deployInfo.getElementsByTagName("URL")[0].firstChild.data
          self.cowdir = deployInfo.getElementsByTagName("COWDir")[0].firstChild.data
          self.osType = xmldoc.getElementsByTagName("Distribution")[0].firstChild.data;
          fsock.close();
          
          self.downloadLink =self.downloadLink.strip();
          self.deployMethod=self.deployMethod.strip()
          self.osType = self.osType.strip()
      
      def parse(self):
          try:
              self.basicParse();
              self.detailParse();
          except Exception ,e:
              raise Exception, "Failed to parse vnode xml: "+str(e); 

##################
